package com.biboheart.sc.lexer;

import com.biboheart.sc.support.definition.TokenType;
import com.biboheart.sc.token.Token;

import java.util.ArrayList;
import java.util.List;

public class LexerBuilder {
    private static volatile LexerBuilder sharedInstance;

    public static LexerBuilder getSharedInstance() {
        LexerBuilder instance = sharedInstance;
        if (null == instance) {
            synchronized (LexerBuilder.class) {
                instance = sharedInstance;
                if (instance == null) {
                    instance = new LexerBuilder();
                    sharedInstance = instance;
                }
            }
        }
        return instance;
    }

    public List<Token> analysis(String content) {
        Lexer lexer = new LexerDefault(content);
        List<Token> list = new ArrayList<>();
        Token token = lexer.nextToken();
        while (null != token && !TokenType.TOKEN_EOF.equals(token.getType())) {
            list.add(token);
            token = lexer.nextToken();
        }
        return list;
    }
}
